package com.smart.campus.web.controller.student;

import com.smart.campus.common.core.controller.BaseController;
import com.smart.campus.common.core.page.TableDataInfo;
import com.smart.campus.school.domain.ScPaymentBill;
import com.smart.campus.school.service.*;
import com.smart.campus.web.utils.StudentUtil;
import org.iherus.codegen.qrcode.SimpleQrcodeGenerator;
import org.iherus.codegen.utils.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.OutputStream;
import java.util.List;

/**
 * 学生信息Controller
 * 
 * @author Zhihe.Li
 * @date 2020-05-18
 */
@Controller
@RequestMapping("/student/pay")
public class StudentPayController extends BaseController
{
    private String prefix = "student";

    @Autowired
    private IStudentService studentService;

    @Autowired
    private IGradeService gradeService;

    @Autowired
    private IClassInfoService classInfoService;
    @Autowired
    private IScPaymentBillService scPaymentBillService;

    @Autowired
    private IWrongQuestionService wrongQuestionService;

    /**
     * 查询缴费单列表
     */
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(HttpServletRequest request) {

        Long userId = StudentUtil.getStudentId();
        if(userId == null){
            return null;
        }
        ScPaymentBill scPaymentBill = new ScPaymentBill();
        scPaymentBill.setUserId(userId);
        scPaymentBill.setStatus(0);
        startPage();
        List<ScPaymentBill> list = scPaymentBillService.selectScPaymentBillList(scPaymentBill);
        return getDataTable(list);
    }

    /**
     * 账单列表
     * @return
     */
    @GetMapping("/bill")
    public String bill() {
        return prefix + "/pay/bill";
    }

    /**
     * 账单列表
     * @return
     */
    @GetMapping("/history")
    public String history() {

        return prefix + "/pay/history";
    }

    /**
     * 查询缴费单列表
     */
    @PostMapping("/historyList")
    @ResponseBody
    public TableDataInfo historyList(ScPaymentBill scPaymentBill) {

        Long userId = StudentUtil.getStudentId();
        if(userId == null){
            return null;
        }
        scPaymentBill.setUserId(userId);
        scPaymentBill.setStatus(1);
        startPage();
        List<ScPaymentBill> list = scPaymentBillService.selectScPaymentBillList(scPaymentBill);
        return getDataTable(list);
    }

    /**
     * 去支付
     */
    //@RequiresPermissions("school:bill:remove")
    @GetMapping("/toPay/{id}")
    public String toPay(@PathVariable("id") Long id, ModelMap mmap) {
        ScPaymentBill scPaymentBill = scPaymentBillService.selectScPaymentBillById(id);
        mmap.put("scPaymentBill", scPaymentBill);
        return prefix + "/pay/toPay";
    }

    @GetMapping("/getPayCode")
    public String getPayCode(HttpServletRequest request, HttpServletResponse response,Long id) {
        HttpSession session = request.getSession();
        response.setDateHeader("Expires", 0);
        response.setHeader("Cache-Control", "no-store, no-cache, must-revalidate");
        response.addHeader("Cache-Control", "post-check=0, pre-check=0");
        response.setHeader("Pragma", "no-cache");
        response.setContentType("image/png");
        OutputStream out = null;

        try {

            String content = "https://baike.baidu.com/item/%E5%97%B7%E5%A4%A7%E5%96%B5/19817560?fr=aladdin";
            out = response.getOutputStream();
            new SimpleQrcodeGenerator().generate(content).toStream(out);

        }catch (Exception e){

        }
        finally {
            IOUtils.closeQuietly(out);
        }

        return null;
    }
}
